<?php

/**************************************************
 Clase      : htmlFormClass
 Descripcion: Realiza renderizado de formularios
 Informacion: Para mayor informacion acerca de 
              esta clase y sus metodos ver la 
              documentacion adjunta.
**************************************************/

class fluxHtml {

   private $frmName             = NULL;
   private $frmMethod           = NULL;
   private static $frmBody      = NULL;
   private static $frmContents  = NULL;

   private $inputs              = NULL;
   private $selects             = NULL;
   private $buttons             = NULL;
   private $tables              = NULL;
	private $textareas           = NULL;
	private $checkboxes          = NULL;
	private $radio 		        = NULL;
	private $hrefs 		        = NULL;


   public function __construct ($name=NULL,$method=NULL,$action=NULL) {


      //Define form name method and action from within the __construct function
      //A begin form method would be kind of useless

      (is_null($name)  || empty($name))     ? $this -> name='default'   : $this -> name="$name";
      (is_null($method)|| empty($method))   ? $this -> method='POST'    : $this -> method="$method";
      (is_null($action)|| empty($action))   ? $this -> action=''        : $this -> action="$action";

      $this -> frmBody = '<form name='. '"' . $this -> name   . '"' . ' ' . 'method=' . '"' .
      $this -> method . '"' .  ' action=' . '"' . $this -> action . '"' . '>';

   }


	public function htmlCheckBox ($name=NULL,$array=NULL,$ID=NULL,$id_texto=NULL,$checked=array()) {

		$CB = "\n";

		while (list($key,$value) = each ($array)) {

			(is_null($id_texto)) ? $TEXTO=$value[$ID] : $TEXTO=$value[$id_texto];

			(is_array ($checked) && in_array($value[$ID],$checked)) ? $x = ' checked' : $x = '';

			$CB .= '<p><input' .  $x  . ' type="checkbox" name="' . $name . '[]' . '" value="' . $value[$ID] . '">' 
             . $TEXTO . '</p>' . "\n";
		}

		$this -> checkboxes[$name] = $CB;

		return;	

	}

	//array (0 => 'http://www.google.com|Texto del medio|_blank|esto es algo para rollover',
   //       1 => 'http://www.yahoo.com'

	public function htmlHrefs ($hrefs=NULL,$clave='hrefs') {

		$c = 0;

		while (list($key,$value) = each ($hrefs)) {

			$partes = explode ('|',$value);

			$url    = $partes[0];  
			$texto  = $partes[1];  
			$target = $partes[2];  
			$alt    = $partes[3];  


			($url)    ? $url    = $url    : $url='';
			($texto)  ? $texto  = $texto  : $texto='';
			($target) ? $target = ' target="' . $target . '"' : $target=' target="_self"';
			($alt)    ? $alt    = ' alt="' . $alt .'"'    : $alt='';

			$retHrefs['link_' . $c] = '<a href="' . $url . '"' . $target . $alt . '>' . $texto . '</a>' . "\n" ;

			$c++;

		}

		$this -> hrefs[$clave] = $retHrefs;

		return;	

	}

	public function DM_htmlHrefsSinTags ($hrefs=NULL,$clave='hrefs') {

		$c = 0;

		while (list($key,$value) = each ($hrefs)) {

			$partes = explode ('|',$value);

			$url    = $partes[0];  
			$texto  = $partes[1];  
			$target = $partes[2];  
			$alt    = $partes[3];  


			($url)    ? $url    = $url    : $url='';
			($texto)  ? $texto  = $texto  : $texto='';
			($target) ? $target = ' target="' . $target . '"' : $target=' target="_self"';
			($alt)    ? $alt    = ' alt="' . $alt .'"'    : $alt='';

			$retHrefs['link_' . $c] = array("url"=>$url,"texto"=>$texto) ;

			$c++;

		}

		$this -> hrefs[$clave] = $retHrefs;

		return;	

	}

   public function htmlFetchHrefs ($href_name=NULL) {

      if (is_null ($href_name) || empty ($href_name))
			return (FALSE);

      if (array_key_exists ($href_name,$this -> hrefs))
		   return ($this -> hrefs[$href_name]);		

			return (FALSE);

   }

   public function DM_htmlFetchHrefs ($href_name=NULL) {

      if (is_null ($href_name) || empty ($href_name))
			return (FALSE);

      if (array_key_exists ($href_name,$this -> hrefs))
		   return ($this -> hrefs[$href_name]);		

			return (FALSE);

   }

	
	public function htmlRadio ($name=NULL,$array=NULL,$ID=NULL,$id_texto=NULL) {

		while (list($key,$value) = each ($array)) {

			(is_null($id_texto)) ? $TEXTO=$value[$ID] : $TEXTO=$value[$id_texto];

			$CB .= '<p><input type="radio" name="' . $name  . '" value="' . $value[$ID] . '">' 
                . $TEXTO . '</p>' . "\n";
		}

		$this -> radio[$name] = $CB;

		return;	

	}


   public function htmlInput ($name=NULL,$value=NULL,$type=NULL,$max=NULL,$order=NULL) {

      (is_null($name)  || empty($name))      ? $name='_NOT_SET'   : $name=$name;
      (is_null($value) || empty($value))     ? $value=''          : $value='value=' . '"' .  $value . '"';
      (is_null($type)  || empty($type))      ? $type='text'       : $type="$type";

      $input = '<input type=' . '"' . $type . '"' . ' ' . 'name=' . '"' .  $name . '"' . ' ' . 'id=' . '"' .  $name . '"' . ' ' . $value;

      //Check if a maxlength was given for this input field 

      (!is_null($max)  && !empty($max))       ? $input .= ' ' . ' maxlength=' . $max : $input=$input;

      $input .= '>';  //Close the tag

      $this -> inputs[$name] = $input;

   }

   public function htmlFetchCheckBox ($checkbox_name=NULL) {

      if (is_null ($checkbox_name) || empty ($checkbox_name))
			return (FALSE);

      if (array_key_exists ($checkbox_name,$this -> checkboxes))
		   return ($this -> checkboxes[$checkbox_name]);		

			return (FALSE);

   }


   public function htmlFetchRadio ($checkbox_name=NULL) {

      if (is_null ($checkbox_name) || empty ($checkbox_name))
			return (FALSE);

      if (array_key_exists ($checkbox_name,$this -> radio))
		   return ($this -> radio[$checkbox_name]);		

			return (FALSE);

   }


   public function htmlFetchSelect ($select_name=NULL) {

      if (is_null ($select_name) || empty ($select_name))
			return (FALSE);

      if (array_key_exists ($select_name,$this -> selects))
		   return ($this -> selects[$select_name]);		

			return (FALSE);

   }

   public function htmlFetchText ($text_name=NULL) {

      if (is_null ($text_name) || empty ($text_name))
			return (FALSE);

      if (array_key_exists ($text_name,$this -> textareas))
		   return ($this -> textareas[$text_name]);		

			return (FALSE);

   }

   public function htmlFetchTable ($table_name=NULL) {

      if (is_null ($table_name) || empty ($table_name))
			return (FALSE);

      if (@array_key_exists ($table_name,$this -> tables))
		   return ($this -> tables[$table_name]);		

			return (FALSE);

   }
   
   public function htmlFetchInput ($input_name=NULL) {

      if (is_null ($input_name) || empty ($input_name))
			return (FALSE);
      if (array_key_exists ($input_name,$this -> inputs))
		   return ($this -> inputs[$input_name]);		

			return (FALSE);

   }

   public function listInputs () {

      $inputs = $this -> inputs;

      echo '<p>*****************</p>'      . "\n" .
           '<p><b>Input Listing:</b></p>'  . "\n" .
           '<p>*****************</p>'      . "\n";

      if (!is_array($this -> inputs)) {
         echo 'No inputs assigned';
         return;
      }

      while (list ($k,$v) = each ($inputs)) 
         echo $k . '=>' . var_dump($v) . "\n";
      
   }

   public function listSelects () {

      $selects = $this -> selects;

      echo '<p>*****************</p>'      . "\n" .
           '<p><b>Select Listing:</b></p>' . "\n" .
           '<p>*****************</p>'      . "\n";

      if (!is_array($this -> selects)) {
         echo 'No selects assigned';
         return;
      }

      while (list ($k,$v) = each ($selects)) 
         echo $k . '=>' . var_dump($v) . "\n";

      
   }

   public function listTables () {

      $tables = $this -> tables;

      echo '<p>*****************</p>'      . "\n" .
           '<p><b>Table Listing:</b></p>'  . "\n" .
           '<p>*****************</p>'      . "\n";

      if (!is_array($this -> tables)) {
         echo 'No tables assigned';
         return;
      }

      while (list ($k,$v) = each ($tables)) 
         echo $k . '=>' . var_dump($v) . "\n";
      
   }

  public function htmlSelect ($name=NULL,$array=NULL,$field=NULL,$llave=NULL,$onChange=NULL,$default=-1,$omit=NULL,$multiple=NULL) {

	if (!is_null ($onChange))
		$onChange=' onChange="'.$onChange.'"';

     if (!is_array ($array) || is_null ($array) )
	     return (FALSE);

     $X=NULL;



     (is_null($name) || empty($name))  ? $name='NOT_SET' : $name="$name";

		if ($multiple)
        $select = '   <select name=' . '"' . $name . '[]"' . ' ' . 'multiple' . $onChange .'>' . "\n";
		else
        $select = '   <select name=' . '"' . $name . '"' . ' id="'.$name. '"' . $onChange . ">\n";

     while (list($key,$value) = each ($array)) {


       (!is_null ($llave)) ? $key=$value[$llave] : $key=$key;

			if (is_array ($omit)) {
				while (list ($K,$V) = each ($omit)) {
					($K == $value[$field]) ? $X = 1 : $X = 0;
					break;
				}
				reset ($omit);
			}
				

			if ($X)
				continue; 

       ($key == $default) ? $x=' selected' : $x='';

       $select .= '      <option' . $x . ' ' . 'value=' . '"' . $key . '"' . '>' . $value[$field] . '</option>' . "\n";

     }

     $select .= '   </select>' . "\n";

     $this -> selects[$name] = $select;    
 
  }

   public function htmlTable ($name=NULL,$array=NULL,$heading=NULL,$omit=NULL,$llave=NULL,$link=NULL) {

      if (!sizeof($array))
         return FALSE; 
     
      $TABLE = NULL;

      $TABLE = '<table cellspacing="0" cellpadding="0">' . "\n" . "   <tr>\n";
     
      if (!is_null ($heading)) { 

		   $AKEYS = array_keys ($array[$llave]);

         foreach ($AKEYS as $header) {
   
      	   if (is_array($omit) && array_key_exists ($header,$omit) ) 
            	continue;

	         $TABLE .= '      <th>' . $header . '</th>' . "\n";

	         $c++;

			}


      	   if (is_array($link)) {

                  while (list($k,$v) = each ($link)) {

                  	if ($k == '_linkey')
								continue;

                     $TABLE .= '      <th><p>' . $k . '</p></th>';

						}

                  reset ($link);

            }

      		$TABLE .= '   </tr>' . "\n";
            

      }

      while (list ($k,$v) = each ($array)) {

         $TABLE .= '   <tr>' . "\n"; 

		   $AKEYS = array_keys ($v);
        
         foreach ($AKEYS as $claves) {

      	   if (is_array($omit) && array_key_exists ($claves,$omit))
            	continue;

            $TABLE .= '         <td>' . '<p>' . $v[$claves] . '</p>' . "</td>\n";

            
            
			}

      	   if (is_array($link)) {

            	if(array_key_exists('_linkey', $link)) {
            		$linkey=$link['_linkey'];
            		$valor=$v[$linkey];
            	} else {
                  $valor='';
               }

					while (list($key,$value) = each ($link)) {

                  if ($key == '_linkey')
							continue;

						$TABLE .= '         <td><a href=' . $value . '/' .  $valor . '>' . $key . '</a></td>' . "\n";

					}
                  reset ($link);

            }

         $TABLE .= '   </tr>' . "\n";
		

      } 

		$TABLE .= '</table>' . "\n";

      $this -> tables[$name] = $TABLE;

   }

   
   public function htmlFormClose () {

      $this -> frmContents .= "</form>\n";
      return;

   }

   public function htmlFormDispatch () {

      echo $this -> frmBody;
      $this -> parseInputs();

      while (list ($key,$value) = each ($this -> frmContents))
         var_dump ($value); 

      return;

   }


	public function htmlText ($name=NULL,$content=NULL) {

		$this -> textareas[$name] = '<textarea name="' . $name . '">' . $content . '</textarea>' . "\n";
		return;

	}

}

?>
